package com.zdp.sources.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdp.sources.common.db.DBTypeEnum;
import com.zdp.sources.common.db.DataSourceSwitch;
import com.zdp.sources.mapper.UserMapper;
import com.zdp.sources.pojo.User;
import com.zdp.sources.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author sesshomaru
 * @date 2022/2/28
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Transactional(rollbackFor = Exception.class, propagation = Propagation.SUPPORTS)
    @Override
    public List<User> select(Page<User> page) {
        return userMapper.selectPage(page, new QueryWrapper<>()).getRecords();
    }

    @DataSourceSwitch(DBTypeEnum.SLAVE1)
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    @Override
    public boolean insert(String username) {
        User user = new User();
        user.setDb("");
        user.setName(username);
        user.setCreateTime(System.currentTimeMillis());
        return userMapper.insert(user) > 0;
    }

    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    @Override
    public boolean delete(Integer id) {
        return userMapper.deleteById(id) > 0;
    }
}
